<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license        
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Guestbook
 *
 * Allow user to write entries in a guestbook
 *
 * @package         conlabz CMS
 * @subpackage      Controllers
 */
class Guestbook extends Public_Controller { 
	function Guestbook( ) {
		parent::Public_Controller( );

		$this->load->helper( 'form' );

		// Load the validation library
		$this->load->library( 'validation' );
		//needed for the guestbook captcha
		$this->load->library("Recaptcha_lib");

		// Load the guestbook model
		$this->load->model( 'guestbook_model' );
		
		// Load the guestbook language module
		$this->lang->module_load( 'guestbook', 'guestbook' );
		
		log_message( 'debug', 'conlabz CMS : Guestbook class loaded' );
	}

	/**
	 * Get Entry Details
	 *
	 * Load entry detail values from the submitted form
	 *
	 * @access private
	 * @return array
	 */
	function _get_entry_details(){
		$data['name'] = $this->input->post( 'name' );
		$data['email'] = $this->input->post( 'email' );
		$data['title'] = $this->input->post( 'title' );
		$data['body'] = $this->input->post( 'body' );

		return $data;
	}
	
	function save( $guestbook_id ){
		$key = $this->guestbook_model->getPrivateKeyById( $guestbook_id );
		$resp = recaptcha_check_answer( $key,
		                                $_SERVER["REMOTE_ADDR"],
		                                $_POST["recaptcha_challenge_field"],
		                                $_POST["recaptcha_response_field"]);
		
		if( !$resp->is_valid ){
			flashMsg( 'error', "The reCAPTCHA wasn't entered correctly. Go back and try it again. (reCAPTCHA said: " . $resp->error . ")" );
			redirect( $_SERVER['HTTP_REFERER'] );
			exit;
		}
		
		// VALIDATION FIELDS
		$fields['id']          	= "ID";
		$fields['name']        	= $this->lang->line( 'guestbook_entry_name'           );
		$fields['body'] 		= $this->lang->line( 'guestbook_entry_body'     );
		
		$this->validation->set_fields( $fields );

		// Setup validation rules
		$rules['name']        = "trim|required";
		
		if( $this->input->post( 'submit' ) ) {
			// Form submitted, check rules
			$this->validation->set_rules( $rules );
		}

		// Save form
		// CREATE
		// Fetch form values
		$guestbook_entry = $this->_get_entry_details( );
		$guestbook_entry['guestbook_id'] = $guestbook_id;
		$guestbook_entry['active'] = '1'; 
		$guestbook_entry['timestamp'] = date( "Y-m-d H:i:s" );
		$this->db->trans_start( );
		$guestbook_entry['id'] = $this->guestbook_model->insert_entry( $guestbook_entry );
		
		if( $this->db->trans_status( ) === TRUE ) {
			$this->db->trans_commit( );
			flashMsg( 'success', sprintf( $this->lang->line( 'userlib_guestbook_entry_saved' ), $guestbook_entry['name'] ) );
		} else {
			$this->db->trans_rollback( );
			flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_create_guestbook_entry' ) ) );
		}
		redirect( $_SERVER['HTTP_REFERER'] );
	}
}
/* End of file guestbook.php */
/* Location: ./modules/guestbook/controllers/guestbook.php */